﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.IO;
namespace Mailer
{
    public class Emailer
    {
        public string To { get; set; }
        public string Subject { get; set; }
        public string MailBody { get; set; }
        public Attachment File { get; set; }
        public MemoryStream FileAttachment { get; set; }
        public Emailer(string to, string subject)
        {
            this.To = to;
            this.Subject = subject;
        }
        public Emailer()
        {

        }

        public bool SendMail()
        {
            try
            {
                MailMessage msg = new MailMessage();
                msg.To.Add(new MailAddress(To));
                msg.From = new MailAddress("admin@ssica.org");
                msg.Bcc.Add(new MailAddress("dharmendra.hbti@gmail.com"));
                msg.Subject = Subject;
                msg.Body = MailBody;
                msg.IsBodyHtml = true;
                if (FileAttachment != null)
                {
                    Attachment _attach = new Attachment(FileAttachment, "Scans.csv");
                    _attach.ContentDisposition.FileName = "Scans.csv";
                    _attach.ContentType = new System.Net.Mime.ContentType("text/csv");
                    _attach.ContentDisposition.Size = FileAttachment.Length;
                    _attach.ContentDisposition.CreationDate = DateTime.Now;
                    msg.Attachments.Add(_attach);
                }
                if (File!=null)
                {
                    msg.Attachments.Add(File);
                }
                SmtpClient _client = new SmtpClient();
                _client.Send(msg);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}